﻿using System;
using System.Net;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ProjectorMapApp
{
    class TimeSlot
    {

        #region declare variables

        int timeSlotID;
        public int TimeSlotID
        {
            get { return timeSlotID; }
            set { timeSlotID = value; }
        }

        bool showFilter = true;
        public bool ShowFilter
        {
            get { return showFilter; }
            set { showFilter = value; }
        }

        ImageSource timeSlotImageSourceSelected;
        public ImageSource TimeSlotImageSourceSelected
        {
            get { return timeSlotImageSourceSelected; }
            set { timeSlotImageSourceSelected = value; }
        }

        ImageSource timeSlotImgUnSelected;
        public ImageSource TimeSlotImgUnSelected
        {
            get { return timeSlotImgUnSelected; }
            set { timeSlotImgUnSelected = value; }
        }


        #endregion

        /// <summary>
        /// creates timeslot
        /// </summary>
        /// <param name="timeSlID">the id of the timeslot</param>
        /// <param name="normalImgURL">Image that represents the timeslot</param>
        public TimeSlot(int timeSlID, string normalImgURL, string unselectImgURL)
        {
            this.timeSlotID = timeSlID;
            this.ShowFilter = true;

            //selected image
            try
            {
                WebClient wcNormalImg = new WebClient();
                System.IO.Stream streamNormalImg = wcNormalImg.OpenRead(MainWindow.iconsStarterPath + normalImgURL);
                System.Drawing.Bitmap tsImgNrml = new System.Drawing.Bitmap(streamNormalImg);
                this.timeSlotImageSourceSelected = GetImage(tsImgNrml);
            }
            catch
            {
                this.timeSlotImageSourceSelected = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/timelinething.png", UriKind.Relative));
            }

            //unselected image
            try
            {
                WebClient wc = new WebClient();
                System.IO.Stream stream = wc.OpenRead(MainWindow.iconsStarterPath + unselectImgURL);
                System.Drawing.Bitmap ts = new System.Drawing.Bitmap(stream);
                this.timeSlotImgUnSelected = GetImage(ts);
            }
            catch
            {
                this.timeSlotImgUnSelected = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/timelinething.png", UriKind.Relative));
            }
        }

        /// <summary>
        /// Convert Bitmap so its appropriate to use as ImageSource
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private System.Windows.Media.Imaging.BitmapImage GetImage(System.Drawing.Bitmap image)
        {
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            System.IO.MemoryStream mem = new System.IO.MemoryStream();
            image.Save(mem, System.Drawing.Imaging.ImageFormat.Png);
            bmp.BeginInit();
            bmp.StreamSource = mem;
            bmp.EndInit();
            return bmp;
        }
    }
}
